import { Component, OnInit, Input } from '@angular/core';
import { ImgSlidePage } from '../slides-imgage/img-slide.page';
import { ModalController, AlertController } from '@ionic/angular';
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
@Component({
  selector: 'app-works-list',
  templateUrl: './works-list.component.html',
  styleUrls: ['./works-list.component.scss'],
})
export class WorksListComponent implements OnInit {
  @Input() tagData: any;
  constructor(
    private modalCtrl: ModalController,
    private httpService: HttpService,
    private appGlobalService: AppGlobalService,
    public alertController: AlertController,
    private pageModalService: PageModalService,
  ) { }

  ngOnInit() {
  }
  // 打开图片展示框，滑动展示
  async openImage(index, imageUrl) {
    const urlArray = [];
    imageUrl.forEach(element => {
      urlArray.push(element);
    });
    const value = { img: urlArray, index: index + 1 };
    const modal = await this.modalCtrl.create({
      component: ImgSlidePage,
      componentProps: { value: JSON.stringify(value) }
    });
    return await modal.present();
  }

  async presentAlertWithMsgBtn(item) {
    if (item.submit) {
      const submit = item.submit;
      const that = this;
      const alert = await this.alertController.create({
        message: submit.content.text,
        buttons: [
          {
            text: "取消",
            role: "cancel",
            cssClass: "secondary"
          },
          {
            text: '确定',
            handler: () => {
              const params = {
                content: submit.content,
                id: submit.id,
                sessionId: localStorage.getItem("sessionId")
              };
              this.httpService.post("application/submit", params, res => {
                if (res.success) {
                  this.appGlobalService.showToast(res.message, 1500, "middle");
                }
              });
            }
          }
        ]
      });
      await alert.present();
    } else {
      this.pageModalService.openPage(item.link)
    }
  }
}
